// packagBuy/pages/marketing/verifyCoupon/collectCouponCenter/collectCouponCenter.js
import { getCouponInfo, couponApi, getParamByScene } from '../../../../../config/config'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusHeight,
    bgimgUrl: app.globalData.BgimgUrl+'couponBgimg.png',
    couponInfoData: '',
    userTypeDlg: false,
    useStartTime: '',
    couponId: '',
    getInto: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var loginInterval=setInterval(()=>{
      if(wx.getStorageSync('loginMsg').token){
        if (options) {
          if (options.couponId) {
            //页面进入
            that.getCoupon(options.couponId)
            that.setData({
              couponId: options.couponId,
              getInto: 'page'
            })
          } else if (options.scene) {
            //二维码进入
            app.reqFun.requestJson("GET", getParamByScene + options.scene, '', (response) => {
              if (response.data) {
                const res = response.data
                if (res.success == 1 && res.data && res.data.couponId) {
                  that.getCoupon(res.data.couponId)
                  that.setData({
                    couponId: res.data.couponId,
                    getInto: 'scene'
                  })
                }
              }
            })
          }
        }
        clearInterval(loginInterval)
      }
    },1000)
    
  },
  goreBack: function (e) { //返回
    wx.navigateBack({
      delta: 1
    })
  },
  goHome: function (e) { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  // 获取优惠券详情
  getCoupon(couponId){
    var that=this;
    app.reqFun.requestJson('GET', getCouponInfo+couponId , '', (response) => {
      const res=response.data
        if (res.success == 1) {
          const couponTimeDisplay = res.data.couponTimeDisplay
          if (!couponTimeDisplay) {
            let useStartTime = res.data.useStartTime
            if (useStartTime) {
              let useStartTimeArr = useStartTime.split('-')
              let day = useStartTimeArr[2].split(' ')
              useStartTime = useStartTimeArr[0] + '年' + useStartTimeArr[1] + '月' + day[0] + '日' + day[1]
            }
            let useEndTime = res.data.useEndTime
            if (useEndTime) {
              let useEndTimeArr = useEndTime.split('-')
              let endDay = useEndTimeArr[2].split(' ')
              useEndTime = useEndTimeArr[0] + '年' + useEndTimeArr[1] + '月' + endDay[0] + '日' + endDay[1]
            }
          that.setData({
            useStartTime: useStartTime,
            useEndTime: useEndTime
          })
      }
      that.setData({
        couponInfoData:res.data
      })
    }
    })
  },
  // 立即领取
  tapCouponOperate(e){
    let that = this
    let userType = that.data.couponInfoData.userType
    let couponId = that.data.couponId
    if (userType == 2) {
      that.setData({ userTypeDlg: true })
      wx.setStorageSync('couponId', couponId)
      wx.setStorageSync('receivePage', 3)
    } else {
      that.reciveCoupon(couponId)
    }
  },
   // 领取优惠券
   reciveCoupon(couponId){
    const params={
      couponId: couponId,
      receivePlatform: 'mini',//领券端口 （pc商城： pc，小程序商城：mini, 门店：store）
      receivePage: 3 // 领取页面：0 商品详情页，1 购物车，2 领券中心 3 单个优惠券领取页
    }
    app.reqFun.requestJson('POST',  couponApi.receiveCoupon, params, (response) => {
      const res=response.data
      if (res.success == 1) {
        wx.showToast({
          title:'领取成功',
          icon: 'none',   // 图标类型，默认success
          duration: 1500   // 提示窗停留时间，默认1500ms
        })
        this.getCoupon(this.data.couponId)
      }else{
        wx.showToast({
          title:res.msg || '领取失败',
          icon: 'none',   // 图标类型，默认success
          duration: 1500   // 提示窗停留时间，默认1500ms
        })
      }
    })
  },
  // 需验证券
  verinfoMat() {
    this.setData({ userTypeDlg: false })
    wx.navigateTo({
      url: '/packagBuy/pages/marketing/verifyCoupon/verInformation/verInformation',
    })
  },
  onClose() {
    this.setData({ userTypeDlg: false });
  },
  productList() {
    let that = this
    let couponId = that.data.couponId
    // 进入商品列表
    wx.navigateTo({
      url: '/packageShop/pages/shopSearch/shopSearch?couponId='+couponId+"&from=coupon"
    })
  },
  changeData() {
    console.log('领券列表')
    // this.onLoad(); //最好是只写需要刷新的区域的代码，onload也可，效率低，有点low
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCoupon(this.data.couponId)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 禁用用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})